﻿// Example header text. Can be configured in the options.
using System;
using System.Data;
using System.Windows.Forms;
using Telerik.WinControls;
using vtech_formBase;
using vtech_frameWorks.Exceptions;

namespace vtech_system.GroupUser
{
    public partial class frmGroupUser : frmListWithGrid
    {
        public frmGroupUser()
        {
            this.InitializeComponent();
        }

        protected override void LoadData()
        {
            ClsGroupUserBS obj = new ClsGroupUserBS();
            this.dsGroupUser.DtGroupUser.Clear();
            this.dsGroupUser.DtGroupUser.Merge(obj.GetAll());
        }

        protected override void Add()
        {
            using (frmAddGroupUser fadd = new frmAddGroupUser())
            {
                fadd.OnAdd += LoadData;
                fadd.ShowDialog();
            }
        }

        private void EditGroupUser()
        {
            try
            {
                this.ID = (Guid)this.dgvGroupUser.SelectedRows[0].Cells[0].Value;
                using (frmEditGroupUser fEdit = new frmEditGroupUser(this.ID))
                {
                    fEdit.OnEdit += LoadData;
                    fEdit.ShowDialog();
                }
            }
            catch
            {
                return;
            }
        }

        protected override void Delete()
        {
            try
            {
                ClsGroupUserBS obj = new ClsGroupUserBS();
                if (this.dgvGroupUser.SelectedRows.Count != 0)
                {
                    this.ID = new Guid(this.dgvGroupUser.SelectedRows[0].Cells[0].Value.ToString());
                    if (DialogResult.Yes == RadMessageBox.Show("Bạn muốn xóa đơn vị này ?", "Thông báo", MessageBoxButtons.YesNo, RadMessageIcon.Question))
                    {
                        DataRow[] drow;
                        drow = obj.GetAll().Select(String.Format("ID = '{0}'", this.ID));
                        foreach (DataRow row in drow)
                        {
                            obj.DoDelete(row);
                        }
                        this.LoadData();
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    ClsInformation.Error("Chưa chọn bản ghi.Không thể xóa!");
                    return;
                }
            }
            catch (Exception ex)
            {
                ClsInformation.Error(ex.ToString());
            }
        }

        protected override void Edit()
        {
            this.EditGroupUser();
        }

        private void girdGroupUser_DoubleClick(object sender, EventArgs e)
        {
            this.Edit();
        }
    }
}